home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / os2tools / bnklysrc / b_spawn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-07  |  6.7 KB  |  172 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software <no-Inc>                   */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          No-Cost<no-tm> Software.                       */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  Copyright (C) 1987, 1988, 1989 by Robert Hartman and Vincent Perriello  */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*            This module was originally written by Bob Hartman             */
  14. /*                                                                          */
  15. /*                                                                          */
  16. /*                        BinkleyTerm "Spawn" module                        */
  17. /*                                                                          */
  18. /*                                                                          */
  19. /*    For complete  details  of the licensing restrictions, please refer    */
  20. /*    to the License  agreement,  which  is published in its entirety in    */
  21. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.210.    */
  22. /*                                                                          */
  23. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  24. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  25. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  26. /*    NOT HAVE THESE FILES,  YOU SHOULD  IMMEDIATELY CONTACT THE AUTHORS    */
  27. /*    AT THE  ADDRESSES LISTED BELOW.  IN NO EVENT SHOULD YOU PROCEED TO    */
  28. /*    USE   THIS  FILE  WITHOUT  HAVING   ACCEPTED  THE  TERMS  OF   THE    */
  29. /*    BINKLEYTERM  LICENSING AGREEMENT,  OR SUCH OTHER  AGREEMENT AS YOU    */
  30. /*    ARE ABLE TO REACH WITH THE AUTHORS.                                   */
  31. /*                                                                          */
  32. /*                                                                          */
  33. /*    The Authors can be reached at the following addresses:                */
  34. /*                                                                          */
  35. /*    Robert C. Hartman                      Vincent E. Perriello           */
  36. /*    Spark Software                         VEP Software                   */
  37. /*    427-3 Amherst Street                   111 Carroll Street             */
  38. /*    CS2032, Suite 232                      Naugatuck, CT 06770            */
  39. /*    Nashua, NH 03061                                                      */
  40. /*                                                                          */
  41. /*    FidoNet 1:132/101                      FidoNet 1:141/491              */
  42. /*    Data    (603) 888-8179                 Data    (203) 729-7569         */
  43. /*                                                                          */
  44. /*    Please feel free to contact us at any time to share your comments     */
  45. /*    about our software and/or licensing policies.                         */
  46. /*                                                                          */
  47. /*--------------------------------------------------------------------------*/
  48.  
  49. #include <stdio.h>
  50. #include <time.h>
  51. #include <stdlib.h>
  52. #include <process.h>
  53. #include <string.h>
  54. #include <dos.h>
  55.  
  56. #ifdef __TURBOC__
  57. #include <dir.h>
  58. #include <mem.h>
  59. #else
  60. #include <direct.h>
  61. #include <memory.h>
  62. #endif
  63.  
  64. #include "com.h"
  65. #include "xfer.h"
  66. #include "zmodem.h"
  67. #include "keybd.h"
  68. #include "sbuf.h"
  69. #include "sched.h"
  70. #include "externs.h"
  71. #include "prototyp.h"
  72.  
  73. #include <stdlib.h> /*PLF extern char **environ; */
  74.  
  75. #ifdef OS_2  /*PLF Fri  05-05-1989  05:19:04 */
  76.  
  77. #define INCL_DOS
  78. #include <os2.h>
  79.  
  80. void b_spawn(char *cmd)
  81. {
  82.     char this_dir[80];
  83.     char *comspec;
  84.  
  85.     getcwd (this_dir, 79);
  86.     if( cmd )
  87.         system(cmd);
  88.     else{
  89.         if(comspec = getenv("COMSPEC"))
  90.             spawnlp(P_WAIT, comspec, comspec, NULL);
  91.     }
  92.     DosSelectDisk(*this_dir - 'A' + 1);       /* in OS/2, drive 1=A, 2=B etc */
  93.     chdir(this_dir);
  94. }
  95.  
  96. #else        /*PLF Fri  05-05-1989  05:19:13 */
  97.  
  98. void b_spawn (cmd_str)
  99. char *cmd_str;
  100. {
  101.    char *p, *p1;
  102.    char **envp, *env, *save, **vp;
  103.    char this_dir[80];
  104.    int ecount;
  105.    unsigned j;
  106.  
  107.    /* Save where we are */
  108.    getcwd (this_dir, 79);
  109.  
  110.    if ((p = getenv ("COMSPEC")) == NULL)
  111.       p = "COMMAND.COM";
  112.  
  113.    if (swapdir == NULL)
  114.       {
  115.       if (cmd_str != NULL)
  116.          system (cmd_str);
  117.       else
  118.          spawnlp (P_WAIT, p, p, NULL);
  119.       }
  120.    else
  121.       {
  122.       /*
  123.        * The following lines are really wierd.  Basically, we need to
  124.        * pass a valid environment to DOS when we do the spawn.  Unfortunately,
  125.        * it is very likely that we have changed the environment (like when
  126.        * we alter the prompt), and therefore we have to make yet another
  127.        * copy of it, and make sure it can be paragraph aligned.  We pass the
  128.        * offset in paragraph format so it can just be added to the DS in
  129.        * order to get the actual paragraph location.
  130.        */
  131.       ecount = 0;
  132.       envp = environ;
  133.  
  134.       /* Count the chars in the environment */
  135.       for (vp = envp; *vp; ecount += strlen (*vp++) + 1)
  136.          ;
  137.  
  138.       /* Add some fudge for nulls, etc */
  139.       ecount += 5;
  140.  
  141.       /* Allocate space for a copy of the environment to be para aligned */
  142.       save = env = malloc (ecount + 15);
  143.  
  144.       /* Align on a paragraph boundary - yucky, but it works */
  145.       *((unsigned int *)&env) = (*((unsigned int *)&env)+0xf) & (~0xf);
  146.  
  147.       p1 = env;
  148.  
  149.       /* Copy old environment to the new */
  150.       for (vp = envp; *vp; vp++)
  151.          p1 = strchr(strcpy(p1, *vp), '\0') + 1;
  152.       *p1++ = '\0';
  153.       *p1++ = '\0';
  154.  
  155.       /* Now actually call our code that does the swapping */
  156.       swapper (p, swapdir, strlen (cmd_str), cmd_str, ((unsigned) (env)) >> 4);
  157.  
  158.       /* Free the stuff we allocated */
  159.       free (save);
  160.       }
  161.  
  162.    /* Go back to the proper directory */
  163.    chdir (this_dir);
  164. #ifdef __TURBOC__
  165.    setdisk ((unsigned) (this_dir[0] - 'A' + 1));
  166. #else
  167.    _dos_setdrive ((unsigned) (this_dir[0] - 'A' + 1), &j);
  168. #endif
  169. }
  170.  
  171. #endif       /*plf Fri  05-05-1989  05:19:21 */
  172.